using UnityEngine;
using System.Collections.Generic;

public class ObstacleManager : BaseBehaviour
{
    public GameObject PrefabObstacle;
    private IList<Obstacle> obstacles;
    private int currentObstacle;
    
    private int CurrentObstacle
    {
        get
        {
            return currentObstacle;
        }
        set
        {
            currentObstacle = value;

            if (currentObstacle >= obstacles.Count)
                currentObstacle = 0;
        }
    }

    private int NextObstacle
    {
        get
        {
            return CurrentObstacle >= obstacles.Count - 1 ? 0 : CurrentObstacle + 1;
        }
    }

    protected override void start()
    {
        createObstacles();
    }

    private void createObstacles()
    {
        ObstacleFactory obstacleFactory = new ObstacleFactory(this);
        obstacles = obstacleFactory.createObstacles();
    }

    protected override void update(float deltaTime)
    {
        if (obstacles[CurrentObstacle].hasPassedTheGap())
        {
            obstacles[NextObstacle].reset();
            CurrentObstacle++;
        }
    }

    public int getCountObstacles()
    {
        ObstacleCounterAtScreen counterObstaclesAtScreen = new ObstacleCounterAtScreen();

        return counterObstaclesAtScreen.getCountObstacles();
    }

    public GameObject createGameObjectObstacle()
    {
        return GameObjectManager.instantiate(PrefabObstacle, transform.position, transform.rotation);
    }
}
